package com.ruoyi.pcwl.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.pcwl.domain.Carousel;
import com.ruoyi.pcwl.domain.CarouselItem;
import com.ruoyi.pcwl.domain.enums.CarouselTypeEnum;
import com.ruoyi.pcwl.domain.vo.CarouselVo;
import com.ruoyi.pcwl.mapper.CarouselItemMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.ruoyi.pcwl.domain.query.CarouselQuery;
import com.ruoyi.pcwl.mapper.CarouselMapper;
import com.ruoyi.pcwl.service.CarouselService;


/**
 * 轮播图信息 业务接口实现
 */
@Service("carouselService")
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

	@Resource
	CarouselItemMapper carouselItemMapper;

	/**
	 * 据条件查询记录数
	 */
	@Override
	public Integer findCountByParam(CarouselQuery reqParam) {
		QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (StringUtils.isNotEmpty(reqParam.getUsername())) {
			wrapper.like("username", reqParam.getUsername());
		}
		if (StringUtils.isNotEmpty(reqParam.getStatus())) {
			wrapper.like("status", reqParam.getStatus());
		}
		if (StringUtils.isNotEmpty(reqParam.getRemark())) {
			wrapper.like("remark", reqParam.getRemark());
		}
		if (StringUtils.isNotEmpty(reqParam.getCarouselName())) {
			wrapper.like("carousel_name", reqParam.getCarouselName());
		}
		Long aLong = baseMapper.selectCount(wrapper);
		return aLong.intValue();
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public IPage<Carousel> selectPage(CarouselQuery reqParam) {
		QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (StringUtils.isNotEmpty(reqParam.getUsername())) {
			wrapper.like("username", reqParam.getUsername());
		}
		if (StringUtils.isNotEmpty(reqParam.getStatus())) {
			wrapper.like("status", reqParam.getStatus());
		}
		if (StringUtils.isNotEmpty(reqParam.getRemark())) {
			wrapper.like("remark", reqParam.getRemark());
		}
		if (StringUtils.isNotEmpty(reqParam.getCarouselName())) {
			wrapper.like("carousel_name", reqParam.getCarouselName());
		}
		if (MapUtils.isNotEmpty(reqParam.getParams()))
		{
			Map<String, Object> params = reqParam.getParams();
			if (params.keySet().contains("beginTime"))
				wrapper.ge("create_date", params.get("beginTime"));
			if (params.keySet().contains("endTime"))
				wrapper.le("create_date", params.get("endTime"));
		}
		return baseMapper.selectPage(reqParam.getPage(), wrapper);
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(Carousel bean) {
		bean.setCreateDate(new Date());
		return baseMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Carousel> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return baseMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
//	@Override
//	public Integer addOrUpdateBatch(List<Carousel> listBean) {
//		if (listBean == null || listBean.isEmpty()) {
//			return 0;
//		}
//		return baseMapper.insertOrUpdateBatch(listBean);
//	}

	/**
	 * 多条件更新
	 */
//	@Override
//	public Integer updateByParam(Carousel bean, CarouselQuery param) {
//		return baseMapper.updateByParam(bean, param);
//	}

	/**
	 * 多条件删除
	 */
//	@Override
//	public Integer deleteByParam(CarouselQuery param) {
//		return baseMapper.deleteByParam(param);
//	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public CarouselVo getCarouselById(Long id) {
		Carousel carousel = baseMapper.selectById(id);
		CarouselVo carouselVo = new CarouselVo();
		BeanUtils.copyProperties(carousel,carouselVo);

		//获取轮播图项信息
		List<CarouselItem> carouselItemList = carouselItemMapper.selectList(new QueryWrapper<CarouselItem>().eq("carousel_id", id));
		carouselVo.setCarouselItemList(carouselItemList);
		return carouselVo;
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateCarouselById(Carousel bean) {
		bean.setChangeDate(new Date());
		return baseMapper.updateById(bean);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteCarouselById(Long id) {
		return baseMapper.deleteById(id);
	}

	@Override
	public CarouselVo getCarousel(CarouselTypeEnum carouselTypeEnum) {
		Carousel carousel = baseMapper.selectOne(new QueryWrapper<Carousel>().eq("category_id", carouselTypeEnum.getCategoryId()).and(wrapper -> wrapper.eq("status", "1")));
		CarouselVo carouselVo = new CarouselVo();
		BeanUtils.copyProperties(carousel,carouselVo);

		//获取轮播图项信息
		List<CarouselItem> carouselItemList = carouselItemMapper.selectList(new QueryWrapper<CarouselItem>().eq("carousel_id", carousel.getId()));
		carouselVo.setCarouselItemList(carouselItemList);
		return carouselVo;
	}

	@Override
	public void publishSyCarousel(Long id) {
		Carousel carousel1 = baseMapper.selectById(id);
		List<Carousel> carouselList = baseMapper.selectList(new QueryWrapper<Carousel>().eq("category_id", carousel1.getCategoryId()));
		List<Long> ids = carouselList.stream().map(Carousel::getId).collect(Collectors.toList());


		UpdateWrapper<Carousel> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("category_id", carousel1.getCategoryId()) // 设置更新条件
				.setSql("status='0'") // 设置需要更新的字段及其值
				.in("id", ids); // 指定要更新的ID列表
		int rowsAffected = baseMapper.update(null, updateWrapper); // null参数表示不修改任何属性

		System.out.println("成功更新 " + rowsAffected + " 条记录");

		Carousel carousel = baseMapper.selectById(id);
		carousel.setStatus("1");
		baseMapper.updateById(carousel);
	}

	/**
	 * 获取首页轮播图
	 * @return
	 */
	@Override
	public List<Carousel> getCarouselCategory() {
		QueryWrapper<Carousel> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("category_id", "category_name");

//		List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
//		System.out.println(maps);


		List<Carousel> carousels = baseMapper.selectList(queryWrapper);

//		System.out.println(carousels);
//		Map<Long, String> collect = carousels.stream().distinct().collect(Collectors.toMap(carousel -> carousel.getCategoryId(), carousel -> carousel.getCategoryName()));
		List<Carousel> collect = carousels.stream().distinct().collect(Collectors.toList());
//		carousels.stream().distinct().forEach(System.out::println);


		return collect;
	}
}
